<template>
  <!-- 对话框 -->
  <el-dialog title="新增员工" :visible="dialogShow" :before-close="cancelFn">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="姓名：" :label-width="formLabelWidth" prop="username">
        <el-input v-model="form.username" autocomplete="off" style="width: 80%;" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机：" :label-width="formLabelWidth" prop="mobile">
        <el-input v-model="form.mobile" autocomplete="off" style="width: 80%;" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间：" :label-width="formLabelWidth" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" type="date" style="width: 80%;" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" />
      </el-form-item>
      <el-form-item label="聘用形式：" :label-width="formLabelWidth" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" style="width: 80%;" placeholder="请选择聘用形式">
          <el-option label="正式" :value="1" />
          <el-option label="非正式" :value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号：" :label-width="formLabelWidth" prop="workNumber">
        <el-input v-model="form.workNumber" autocomplete="off" style="width: 80%;" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门：" :label-width="formLabelWidth" prop="departmentName">
        <el-input v-model="form.departmentName" autocomplete="off" style="width: 80%;" placeholder="请输入部门" @focus="treenDataFn" />
        <el-tree v-show="treeShow" default-expand-all :data="treeForm" :props="{ label: 'name'}" @node-click="treeFn" />
      </el-form-item>
      <el-form-item label="转正时间：" :label-width="formLabelWidth" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" style="width: 80%;" placeholder="选择日期" format="yyyy 年 MM 月 dd 日" value-format="yyyy-MM-dd" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancelFn">取 消</el-button>
      <el-button type="primary" @click="submitFn">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartmentsAPI } from '@/api/treetools'
import { adduserAPI } from '@/api/employees'
import { tranlistTotreeData } from '@/utils'
export default {
  props: {
    dialogShow: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      formLabelWidth: '120px', // 表单标题长度
      form: {}, // 对话框对象,
      rules: { // 校验规则
        username: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { pattern: /^1[3-9]\d{9}$/, message: '请输入正确手机号', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: 'blur' }
        ]
      },
      treeForm: [], // 树形数组
      treeShow: false // 树形数组显示隐藏
    }
  },
  methods: {
    submitFn() { // 表单提交事件
      this.$refs.form.validate(async(vald) => {
        if (vald) {
          // 校验成功
          await adduserAPI(this.form)
          this.$bus.$on('submitOkFn')
          // 清空表单
          this.cancelFn()
        } else {
          return false
        }
      })
    },
    async treenDataFn() { // 获取部门列表
      const { depts } = await getDepartmentsAPI()
      this.treeForm = tranlistTotreeData(depts, '')
      this.treeShow = true
    },
    treeFn(data, node, vc) { // 树形节点的点击事件
      this.form.departmentName = data.name
      this.treeShow = false
    },
    cancelFn() { // 取消事件
      this.$bus.$emit('ondialogShow', false)
      this.$refs.form.resetFields()
      this.form = {}
    }
  }
}
</script>

<style>
</style>
